package cn.xtcode.domain.activity.service.discount;

import cn.xtcode.domain.activity.adapter.repository.IActivityRepository;
import cn.xtcode.domain.activity.model.valobj.DiscountTypeEnum;
import cn.xtcode.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @Author xt-code
 * @Description:
 * @Create 2025/5/7 10:56
 */
@Slf4j
public abstract class AbstractDiscountCalculateService implements IDiscountCalculateService {
    @Resource
    protected IActivityRepository repository;

    @Override
    public BigDecimal calculate(String userId, BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
        //1.人群标签过滤
        if (DiscountTypeEnum.TAG.equals(groupBuyDiscount.getDiscountType())) {
            boolean isCrowdRange = filterTagId(userId, groupBuyDiscount.getTagId());
            if (!isCrowdRange) {
                log.info("折扣优惠计算拦截，用户不再优惠人群标签范围内 userId:{}", userId);
                return originalPrice;
            }
        }

        //2.折扣优惠预算
        return doCalculate(originalPrice, groupBuyDiscount);
    }

    /**
     * @param userId 用户ID
     * @param tagId  标签ID
     * @return 判断该用户是否可以进行拼团
     * true可以参与拼团
     */
    private boolean filterTagId(String userId, String tagId) {

        //判断用户是否是该标签标记的人
        return repository.isTagCrowRange(tagId, userId);
    }

    //供子类实现的
    protected abstract BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount);
}
